var base64 = new Base64();
var vm = new Vue({
    el: '#app',
    data() {
        return {
            imglist: ['0', '0', '0', '0'],
            myArray: '',
            orderStatus: {
                PENDING: 0,
                CHECK_PENDING: 1,
                UNACCEPTED: 2,
                ACCEPTED: 3,
                TRADE_SUCCESS: 4,
                TRADE_CLOSE: 5,
                WAIT_FOR_SERVICE: 6
            },
            typeList: [],
            carType: '0',
            photoBeforeCount: 0,
            photoAfterCount: 0,
            beforeIsok:false
        }
    },
    methods: {
        init: function() {
            var curorder = api.getGlobalData({
                key: 'curOrder'
            });
            vm.photoBeforeCount = curorder.photosBeforeCarWash.length;
            vm.photoAfterCount = curorder.photosAfterCarWash.length;
            if (vm.photoBeforeCount>0) {
              vm.beforeShowPic();
            }
          },
          hasLogin: function() {
            var technicianid = api.getGlobalData({
                key: 'technicianid'
            });
            if (technicianid = '') {
                api.setFrameGroupIndex({
                    name: 'IndexGroup',
                    index: 2,
                    reload: true
                });
            }
          },
          beforeShowPic: function() {
            var curorder = api.getGlobalData({
                key: 'curOrder'
            });
            vm.photoBeforeCount = curorder.photosBeforeCarWash.length;
          //  console.log('imageUrl1: ' + this.getImgurl(curorder.photosBeforeCarWash[0].url));
            if (vm.photoBeforeCount>0) {
              document.getElementById('afront').src = this.getImgurl(curorder.photosBeforeCarWash[0].url); //图片链接
              document.getElementById('aleft').src = this.getImgurl(curorder.photosBeforeCarWash[1].url);
              document.getElementById('aright').src = this.getImgurl(curorder.photosBeforeCarWash[2].url);
              document.getElementById('aback').src = this.getImgurl(curorder.photosBeforeCarWash[3].url);
            }
          },
          afterShowPic: function() {
            var curorder = api.getGlobalData({
                        key: 'curOrder'
                    });
            vm.photoAfterCount = curorder.photosAfterCarWash.length;
            if (vm.photoAfterCount>0) {
              document.getElementById('afront').src = this.getImgurl(curorder.photosAfterCarWash[0].url); //图片链接
              document.getElementById('aleft').src = this.getImgurl(curorder.photosAfterCarWash[1].url);
              document.getElementById('aright').src = this.getImgurl(curorder.photosAfterCarWash[2].url);
              document.getElementById('aback').src = this.getImgurl(curorder.photosAfterCarWash[3].url);
            }
          },
          getImgurl: function(url) {
            var imgurl = url;
            var serverUrlImg = 'http://test.youxi1027.com/admin/'
            return  serverUrlImg+imgurl.substr(1)
          },
        showAction: function(idname) {
            api.actionSheet({
                title: '上传照片',
                cancelTitle: '取消',
                buttons: ['拍照', '从手机相册选择']
            }, function(ret, err) {
                if (ret) {
                    vm.getPicture(ret.buttonIndex, idname);
                }
            });
        },
        getPicture: function(sourceType, idname) {
            var params = {};
            if (sourceType == 1) { // 拍照
                params = {
                    sourceType: 'camera',
                    encodingType: 'jpg',
                    mediaValue: 'pic',
                    allowEdit: false,
                    destinationType: 'url',
                    quality: 100,
                    targetWidth: 200,
                    targetHeight: 200,
                    saveToPhotoAlbum: true
                };
            } else if (sourceType == 2) { // 从相机中选择
                params = {
                    sourceType: 'library',
                    encodingType: 'jpg',
                    mediaValue: 'pic',
                    destinationType: 'url',
                    quality: 100,
                    targetWidth: 200,
                    targetHeight: 200
                };
            } else {
                return;
            }
            api.getPicture(params, function(ret, err) {
                if (ret.data) {
                    api.showProgress({
                        style: 'default',
                        animationType: 'fade',
                        text: '图片上传中...',
                        modal: false
                    });
                    document.getElementById(idname).src = ret.data; //图片链接
                    $api.uploadFile(function(ret, err) {
                        if (ret) {
                            vm.addImgList(idname, ret.id); // 添加图片id到列表
                        } else {
                            console.log(JSON.stringify(err))
                            console.log("失败")
                        }
                    }, ret.data)
                    api.hideProgress();
                }
            });
        },
        addImgList: function(idname, id) {
            if (idname == 'afront') {
                vm.imglist.splice(0, 1, id)
            };
            if (idname == 'aleft') {
                vm.imglist.splice(1, 1, id)
            };
            if (idname == 'aright') {
                vm.imglist.splice(2, 1, id)
            };
            if (idname == 'aback') {
                vm.imglist.splice(3, 1, id)
            };
        },
        hasNullFile: function() {
            for (var i = 0; i < vm.imglist.length; i++) {
                if (vm.imglist[i] == '0') {
                    alert('第' + (Number(i) + 1) + '张图片出现异常，请重新上传。')
                    return true;
                }
                if (i <= 2) {
                    vm.myArray = vm.myArray + vm.imglist[i] + ',';
                } else {
                    vm.myArray = vm.myArray + vm.imglist[i];
                }

            }
            return false;
        },
        bindPhoto: function(status, list, callback) {
            var curorder = api.getGlobalData({
                key: 'curOrder'
            });
            params = new Object();
            params.order = curorder.id;
            params.status = status;
            params.photos = list;
            if (list) {
                $api.ajax(function(ret, err) {
                    if (ret) {
                        callback(ret, err);
                    }
                }, 'order/bindingPhoto', 'post', params, 'text');
            }
        },
        updateOrder: function(order, callback) {
            $api.ajax(function(ret, err) {
                if (ret) {
                    callback(ret, err);
                }
            }, 'order/updateOrder', 'post', order, 'json');
        },
        getCarType: function() {
            $api.ajax(function(ret, err) {
                console.log('==getCarType' + JSON.stringify(ret));
                if (ret) {
                    vm.typeList = ret;
                    vm.getCar();
                }
            }, 'car/getCarType', 'post', {}, 'json');
        },
        getCar: function() {
            var order = api.getGlobalData({
                key: 'curOrder'
            });
            var params = new Object();
            params.member = order.memberId;
            $api.ajax(function(ret, err) {
                if (ret) {
                    var car = ret.filter(function(car) {
                        return car.member == order.memberId && order.plateNum == car.plateNum && order.phoneNum == car.car_owner_telephone;
                    });
                    api.setGlobalData({
                        key: 'curCar',
                        value: car[0]
                    });
                    if (typeof car[0].car_type != 'undefined') {
                      vm.carType = car[0].car_type + ':' + car[0].car_type_name;
                    } else {
                      vm.carType = '0';
                    }
                        console.log('==getCar' + vm.carType);
                }
            }, 'car/list', 'post', params, 'json');
        },
        saveCar: function(callback) {
            console.log('==saveCar' + vm.carType);
            if (vm.carType == '0') {
                alert('请选择车型');
            } else {
                var curCar = api.getGlobalData({
                    key: 'curCar'
                });
                curCar.car_type = vm.carType.split(':')[0];
                curCar.car_type_name = vm.carType.split(':')[1];
                $api.ajax(function(ret, err) {
                    if (ret) {
                      console.log('==ret' + JSON.stringify(ret));
                    }
                }, 'car/update', 'post', curCar, 'json');
                callback();
            }
        }
    }
});
